import json
from pprint import pprint
from random import randint
from typing import Generator

nested = [
    "1",
    [
        "1.1",
        "1.2",
        [
            "1.1.1",
            "1.1.2"
        ]
    ],
    [
        2,
        2
    ],
    [
        3,
        3,
        [
            3.1,
            [
                3.2,
                3.2,
                [
                    3.3,
                    3.3,
                    3.3
                ]
            ]
        ]
    ]
]

# nested = [1, [1.1]]

# print(json.dumps(nested, indent=4))

def flatten(nested) ->Generator:
    try:
        try:
            nested + ''
        except TypeError:
            pass
        else:
            # 不迭代类似于字符串的对象：
            raise TypeError

        for sublist in nested:
            for element in flatten(sublist):
                yield element
    except TypeError:
        yield nested # return iterator that include itself, when nested is string or non-iterable object.


f = flatten(nested)

while True:
    try:
        print(next(f), end=" ")
    except StopIteration:
        print()
        break
